//************************************************************************
/// <copyrigth>2024-2025 Corporation.All Rights Reserved</copyrigth>
/// <author>tangyingzhong</author>
/// <contact>tangyz114987@outlook.com</contact>
/// <version>v1.0.0</version>
/// <describe>
/// It is handler to handle notification
/// Note: you need to copy arg for your own at first when you receive the notification
///</describe>
/// <date>2024/5/6</date>
///***********************************************************************
#ifndef IHANDLER_H
#define IHANDLER_H

#include <algorithm>
#include "INotifier.h"
#include "ModuleLoader/ModuleConfig.h"

namespace SystemTool
{
    struct NArgs
    {
        // Sender
        void* pSender = nullptr;

        // Receiver
        void *pReceiver = nullptr;

        // Args(memory managed by notifier)
        void* pArgs = nullptr;
        int iArgSize = 0;

        // Reserved(memory managed by notifier)
        void *pReserved = nullptr;
        int iReservedSize = 0;

        void CreateArgMemory()
        {
            pArgs = new char[iArgSize];
            std::memset(pArgs, 0, iArgSize);
        }

        void CreateReservedMemory()
        {
            pReserved = new char[iReservedSize];
            std::memset(pReserved, 0, iReservedSize);
        }

        void CopyMemory(char *pDestBuf, char *pSrcBuf, int iSrcBufSize)
        {
            std::copy(pSrcBuf, pSrcBuf + iSrcBufSize, pDestBuf);
        }

        void DestroyMemory()
        {
            if (pArgs)
            {
                delete[] (char*)pArgs;
                pArgs = nullptr;
            }
            if (pReserved)
            {
                delete[] (char*)pReserved;
                pReserved = nullptr;
            }
        }
    };

    class CLASS_ITEM IHandler
    {
    public:
        // Destruct the obj
        virtual ~IHandler() {}

        // Config the handler
        virtual void Config(INotifier *pNotifier) = 0;

        // Recv the notification
        virtual void Recv(NArgs &Args) = 0;
    };
}

#endif // IHANDLER_H
